﻿#region References
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ness.Engine.GUI;
using Microsoft.Xna.Framework;
using LuaInterface;
#endregion

namespace Ness.Engine.Interact
{
    /// <summary>
    /// One-time event triggers allow custom game events
    /// </summary>
    class EventTrigger
    {
        #region Public variables
        public Vector3 minimum;
        public Vector3 maximum;
        public bool isActivated;
        public LuaFunction luaFunction;
        public string name;
        #endregion

        /// <summary>
        /// Initialise the Event trigger
        /// </summary>
        /// <param name="name">string name, used for identifying only</param>
        /// <param name="min">Minimum bounds to be within</param>
        /// <param name="max">Maximum bounds to be within</param>
        /// <param name="trigger">Lua function callback when within bounds</param>
        public EventTrigger(string name, Vector3 min, Vector3 max, LuaInterface.LuaFunction trigger)
        {
            //Set min/max bounds
            minimum = min;
            maximum = max;
            this.name = name;

            //Set trigger to call when acitvated
            luaFunction = trigger;
        }

        public void CheckActivate(Vector3 position)
        {
            if (isActivated) return;
            //Are we inside on the X?
            if (position.X > minimum.X && position.X < maximum.X)
            {
                if (position.Y > minimum.Y && position.X < maximum.Y)
                {
                    if (position.Z > minimum.Z && position.Z < maximum.Z)
                    {
                        Debugger.WriteLua("Event trigger activated and disabled (" + name + ")");
                        luaFunction.Call();
                        isActivated = true;
                        luaFunction.Dispose();
                    }
                    else
                    {
                        isActivated = false;
                    }
                }
                else
                {
                    isActivated = false;
                }
            }
            else
            {
                isActivated = false;
            }
        }
    }
}
